package com.xy.controller.admin;

import com.xy.domain.CommonResult;

import com.xy.domain.LoginDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/judge")
@RefreshScope
public class JudgeController {
    @Value("${judge-info.username}")
    private String username;
    @Value("${judge-info.password}")
    private String password;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpServletResponse response;

    @GetMapping("/login")
    public CommonResult loginJudgeServer(String param) {
        LoginDto loginDto = new LoginDto();
        loginDto.setUsername(username);
        loginDto.setPassword(password);
        String url = "http://124.222.169.136:6688/api/admin/login";
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(url, loginDto, CommonResult.class);

        response.setHeader("Authorization", entity.getHeaders().get("Authorization").get(0)); //放到信息头部
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        CommonResult result = entity.getBody();
        result.setCode(222);
        return result;
    }

}
